import wx
import  wx.lib.editor    as  editor
import threading

#class Append(threading.Thread):
#    def __init__(self,parent,myEditor):
#        threading.Thread.__init__(self)
#        self.parent = parent
#        self.myEditor = myEditor
#    
#    def run(self):
#        for i in range(1000):
#            print'..............',i
#            self.myEditor.SingleLineInsert("Le minh luan"+str(i))
#            self.myEditor.BreakLine(None)
#            self.myEditor.AdjustScrollbars()
#            self.myEditor.UpdateView()
##            self.myEditor.UnTouchBuffer()
#            self.myEditor.CheckMaxLength()
#        print '.........end.............'
#        self.myEditor.ShowPosition()
##        self.myEditor.BreakLine(None)
##        self.myEditor.UpdateView()
##        for i in range(1):
##            if not self.myEditor.IsLine(i):
###                self.myEditor.BreakLine(None)
##                print 'break'
##            self.myEditor.SingleLineInsert("Le minh luan")
#            
##            self.text.Update()
#        
#        print 'finished'
 
class CustomEditor(editor.Editor):   
    def __init__(self, parent, id,
                 pos=wx.DefaultPosition, size=wx.DefaultSize, style=0):
        super(CustomEditor,self).__init__( parent, id,
                 pos=wx.DefaultPosition, size=wx.DefaultSize, style=0) 
        self.maxLines = 1200 
    
    def GetLastPosition(self):
        return len(self.lines)
    
    def NumberOfLines(self):
        return len(self.lines)
    
    def GetInsertionPoint(self):
        return 
    
    def SetInsertionPoint(self, (x, y)):
        self.cx = x
        self.cy = y
        self.DrawCursor(None)
    
    def DeleteTopLines(self):
        for i in range(400):
            if len(self.lines)>0:
                self.DeleteTopLine()
                
#        self.AdjustScrollbars()
        self.UpdateView(None)
    
    def DeleteTopLine(self):
        if self.cy > 0:
            self.cy -= 1
        self.lines.pop(0)
    
    def CheckMaxLength(self):
        if self.IsMax():
            self.DeleteTopLines()
    
    def IsMax(self):
        if self.LinesInFile() >= self.maxLines:
            return True
        return False
    
    def InsertLine(self,text):
        pass
    
    def UpdateInsertLineView(self):   
        pass  

    def SingleLineInsert(self, newText):
        print 'SingleLineInsert'
        ModLine = self.GetTextLine(self.cy)
        ModLine = ModLine[:self.cx] + newText + ModLine[self.cx:]
        self.SetTextLine(self.cy, ModLine)
        self.cHoriz(len(newText))
        self.TouchBuffer()
        self.UpdateView()
        
    def OnScroll(self, event):
        print 'canscroll',self.CanScroll()
        dir = event.GetOrientation()
        eventType = event.GetEventType()
        if dir == wx.HORIZONTAL:
            self.HorizScroll(event, eventType)
        else:
#            if self.IsScrollVert():
            print 'IsScrollVert',self.IsScrollVertDown()
            self.VertScroll(event, eventType)
        
    
    def VertScroll(self, event, eventType):
        print 'VertScroll'
        self.ShowPosition()
        if   eventType == wx.wxEVT_SCROLLWIN_LINEUP:
            if not self.IsScrollVertUp():
                return
            self.sy -= 1
        elif eventType == wx.wxEVT_SCROLLWIN_LINEDOWN:
            if not self.IsScrollVertDown():
                return
            self.sy += 1
        elif eventType == wx.wxEVT_SCROLLWIN_PAGEUP:
            self.sy -= self.sh
        elif eventType == wx.wxEVT_SCROLLWIN_PAGEDOWN:
            self.sy += self.sh
        elif eventType == wx.wxEVT_SCROLLWIN_TOP:
            self.sy = self.cy = 0
        elif eventType == wx.wxEVT_SCROLLWIN_BOTTOM:
            self.sy = self.LinesInFile() - self.sh
            self.cy = self.LinesInFile()
        else:
            self.sy = event.GetPosition()

        self.VertBoundaries()
        self.UpdateView()
    
    def IsScrollVertDown(self):
        if self.LinesInFile() <= self.sh:
            return False
        if self.LinesInFile() - self.sy - 1  <= self.sh:
            return False
        return True
    
    def IsScrollVertUp(self):
        if self.sy == 0:
            return False
        return True
    
    def UpdateView(self, dc = None):
        print 'UpdateView'
        if dc is None:
            dc = wx.ClientDC(self)
        if dc.Ok():
            self.SetCharDimensions()
            self.KeepCursorOnScreen()
            self.DrawSimpleCursor(0,0, dc, True)
            self.Draw(dc) 
    
    def OnPaint(self, event):
        print 'OnPaint'
        dc = wx.PaintDC(self)
        if self.isDrawing:
            return
        self.isDrawing = True
        dc.Clear()
        self.UpdateView(dc)
        wx.CallAfter(self.AdjustScrollbars)
        self.isDrawing = False
    
    def Draw(self, odc=None):
        print 'draw'
        if not odc:
            odc = wx.ClientDC(self)

        dc = wx.BufferedDC(odc)
        if dc.IsOk():
            dc.SetFont(self.font)
            dc.SetBackgroundMode(wx.SOLID)
            dc.SetTextBackground(self.bgColor)
            dc.SetTextForeground(self.fgColor)
            dc.Clear()
            for line in range(self.sy, self.sy + self.sh):
                self.DrawLine(line, dc)
#            if len(self.lines) < self.sh + self.sy:
#                self.DrawEofMarker(dc)
            self.DrawCursor(dc)
    
    def ShowPosition(self):
        print "ShowPosition"
        print 'CalcMaxLineLen',self.CalcMaxLineLen()
        print 'fw',self.fw
        print 'fh',self.fh
        print 'cx',self.cx 
        print 'cy',self.cy 
        print 'oldCx',self.oldCx 
        print 'oldCy',self.oldCy 
        print 'sx',self.sx 
        print 'sy',self.sy 
        print 'sw',self.sw 
        print 'sh',self.sh 
        print 'sco_x',self.sco_x 
        print 'sco_y',self.sco_y
        print '...............'

#class notepad(wx.Frame):
#    
#    def __init__(self):
#        wx.Frame.__init__(self, None, -1, "Notepad", size=(800,600))
#        self.panel = wx.Panel(self)
#        
#        self._ImageList = wx.ImageList(16, 16)
#        self._ImageList.Add(wx.Bitmap("image/connect.png"))
#        
##        bookStyle = fnb.FNB_NODRAG
##        bookStyle &= ~(fnb.FNB_NODRAG)
##        bookStyle |= fnb.FNB_ALLOW_FOREIGN_DND 
##        self.nb = fnb.FlatNotebook(self.panel, wx.ID_ANY,size=(800,600), agwStyle=bookStyle)
##        self.nb.SetPadding(wx.Size(10,100))
#        self.nb = CustomsFlatNotebook(self.panel,size=(1000,1000))
#        self.nb.SetImageList(self._ImageList)
#
#        self.flagAddTab = False
#        self.text = CustomEditor(self.nb, -1, style=wx.SUNKEN_BORDER)
#        self.text.SetMaxSize((-1,1100))
##        self.text.SetMinSize((800,600))
#        self.text.Bind(wx.EVT_RIGHT_DOWN, self.append)
#        self.nb.AddPage(self.text, "page1")
##        self.text.UpdateView(dc)
#        
##        imageID = wx.NewId()
##        wx.StaticBitmap(self.panel)
#        image = random.randint(0, 0)
#        sizer = wx.BoxSizer()
#        self.nb.SetSelection(0)
#        sizer.Add(self.nb, 1, wx.EXPAND)
#        self.panel.SetSizer(sizer)
##        self.text.SetText(lines)
#
#    
#        
#    def append(self,event):
#        print '......................'
#        print self.text.cx 
#        print self.text.cy 
#        print self.text.oldCx 
#        print self.text.oldCy 
#        print self.text.sx 
#        print self.text.sy 
#        print self.text.sw 
#        print self.text.sh 
#        print self.text.sco_x 
#        print self.text.sco_y
##        self.text.SingleLineInsert("Le minh luan") 
##        print 'appendevent'
#        aa = Append(self, self.text)
#        aa.run()
##        for i in range(100):
##            self.text.SetTextLine(i,"leminhluan")
##            self.text.UpdateView(None)
##            self.text.MoveEnd(None)
##            self.text.BreakLine(None)
##        wx.MessageBox("Cannot read file update", "Notification1", wx.OK)
#    def EventReturn(self):
#        ev = wx.KeyEvent(wx.wxEVT_CHAR)
#        ev.m_keyCode = wx.WXK_RETURN
#        self.text.ProcessEvent(ev)
#        
#    def tabClose(self,event):
#        print "tabClose", self.nb.GetPageCount(),event.GetSelection(),self.nb.GetSelection()
#        event.Skip()
#        return
#        if self.nb.GetPageCount() == event.GetSelection()+2:
#            self.flagAddTab = True
#            event.Skip() 
#        else:
#            self.flagAddTab = False
#            event.Skip()            
#        #event.Skip()
#        
#app = wx.PySimpleApp()
#notepad().Show()
#app.MainLoop()

